package com.meilai.project.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.middleware.Base64Param;
import com.meilai.project.interceptor.NoAuth;
import com.meilai.project.service.middleware.AttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;

//import org.springframework.beans.factory.support.ConstructorResolver;
@Api(value = "附件", description = "附件的上传")
@RestController
@RequestMapping("api/app/attachment")
public class AttachmentController {

    @Autowired
    private AttachmentService attachmentService;

    @NoAuth
    @ApiOperation(value = "上传(匿名)")
    @PostMapping("/upload-anon")
    public CommonResult uploadImg(@RequestParam("file") String base64) throws IOException {
//        if (file.getSize() > 5 * 1024 * 1024) { //限制大小为5M
//            return new CommonResult().failed("文件超出大小限制");
//        }
//        String originalFilename = file.getOriginalFilename();
//        String url = attachmentService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);

        // 获取图片格式
        String suffix = base64.substring(11, base64.indexOf(";"));
        // 使用插件传输产生的前缀
        String prefix = base64.substring(0, base64.indexOf(",") + 1);
        // 替换前缀为空
        base64 = base64.replace(prefix, "");

        byte[] imageByte = Base64.getDecoder().decode(base64);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageByte);

        String url = attachmentService.storeSimple(byteArrayInputStream, "a.jpg");
        return new CommonResult().success(url);
    }

    @ApiOperation(value = "上传")
    @PostMapping("")
    public CommonResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = attachmentService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
        return new CommonResult().success(url);
    }

    @ApiOperation(value = "删除")
    @PostMapping("del/{fileName}")
    public CommonResult del(@PathVariable("fileName") String fileName) {
        return new CommonResult().success(attachmentService.deleteOne(fileName));
    }

    @ApiOperation(value = "上传base64")
    @PostMapping("base64")
    public CommonResult uploadBase64(@RequestBody Base64Param base64Param) throws IOException {

        String imageString = base64Param.getFile();
        // 使用前端插件时可能有前有（"data:image/xxxx;base64,"）
        // 获取图片格式
        String suffix = imageString.substring(11, imageString.indexOf(";"));
        // 使用插件传输产生的前缀
        String prefix = imageString.substring(0, imageString.indexOf(",") + 1);
        // 替换前缀为空
        imageString = imageString.replace(prefix, "");
        // imageString = imageString.substring(imageString.indexOf(",") + 1);

        byte[] imageByte = Base64.getDecoder().decode(imageString);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageByte);

        String url = attachmentService.storeSimple(byteArrayInputStream, "a.jpg");
        return new CommonResult().success(url);
    }

//    @ApiOperation(value = "上传文件带进度条")
//    @PostMapping(value = "uploadProgressBar")
//    public CommonResult uploadProgressBar(@RequestParam(value = "file") MultipartFile file, HttpSession session, HttpServletRequest request) {
//        try {
//            String url = attachmentService.storeToll(file, session, request);
//            return new CommonResult().success(url);
//        } catch (Exception e) {
//            return new CommonResult().failed(e.getMessage());
//        }
//    }

    @ApiOperation(value = "上传附带uid")
    @PostMapping("{uid}")
    public CommonResult uploadByUid(@RequestParam("file") MultipartFile file, @PathVariable("uid") String uid) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = attachmentService.store(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("uid", uid);
        jsonObject.put("url", url);
        return new CommonResult().success(jsonObject);
    }
}
